﻿using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace Marbles.Core.Web.Controls
{
    public static class TableBuilder
    {

        public static Table CreateTable(string ID)
        {
            Table tbl = new Table();
            tbl.ID = ID;
            return tbl;
        }

        public static Table CreateTable(object[][] Values, string ID, bool firstRowIsHeader)
        {
            int rowIndex = 0;
            int cellIndex = 0;
            Table tbl = CreateTable(ID);
            foreach (object[] rowvalue in Values)
            {    
                cellIndex = 0;
                if ((firstRowIsHeader) && (rowIndex == 0))
                {
                    TableHeaderRow HeaderRow = CreateHeaderRow(string.Format("HeaderRow_{0}", rowIndex));
                    foreach (object cell in rowvalue)
                    {
                        HeaderRow.Cells.Add(CreateHeaderCell(string.Format("HeaderCell_{0}", cellIndex), cell.ToString()));
                        cellIndex += 1;
                    }

                    tbl.Rows.Add(HeaderRow);
                }
                else
                {
                    TableRow row = CreateRow(string.Format("row_{0}", rowIndex));
                    foreach (object cell in rowvalue)
                    {
                        row.Cells.Add(CreateCell(string.Format("cell_{0}", cellIndex), cell.ToString()));
                        cellIndex += 1;
                    }


                    tbl.Rows.Add(row);
                }

                rowIndex += 1;
            }
            return tbl;
        }

        public static Table CreateTable(object[] Values, string ID, bool firstRowIsHeader)
        {
            int rowIndex = 0;
            Table tbl = CreateTable(ID);
            foreach (object rowvalue in Values)
            {
                if ((firstRowIsHeader) && (rowIndex == 0))
                {
                    TableHeaderRow row = CreateHeaderRow(string.Format("row_{0}", rowIndex));
                    row.Cells.Add(CreateHeaderCell(string.Format("cell_{0}", rowIndex), rowvalue.ToString()));
                    tbl.Rows.Add(row);
                    rowIndex += 1;
                }
                else
                {
                    TableRow row = CreateRow(string.Format("row_{0}", rowIndex));
                    row.Cells.Add(CreateCell(string.Format("cell_{0}", rowIndex), rowvalue.ToString()));
                    tbl.Rows.Add(row);
                    rowIndex += 1;
                }
            }
            return tbl;
        }

        public static TableRow CreateRow(string ID)
        {
            TableRow row = new TableRow();
            if (ID != string.Empty) { row.ID = ID; }
            return row;
        }

        public static TableHeaderRow CreateHeaderRow(string ID)
        {
            TableHeaderRow row = new TableHeaderRow();
            if (ID != string.Empty) { row.ID = ID; }
            return row;
        }

        public static TableFooterRow CreateFooterRow(string ID)
        {
            TableFooterRow row = new TableFooterRow();
            if (ID != string.Empty) { row.ID = ID; }
            return row;
        }

        public static TableHeaderCell CreateHeaderCell(string ID)
        {
            TableHeaderCell cell = new TableHeaderCell();
            if (ID != string.Empty) { cell.ID = ID; }
            return cell;
        }

        public static TableHeaderCell CreateHeaderCell(string ID,string text)
        {
            TableHeaderCell cell = new TableHeaderCell();
            if (ID != string.Empty) { cell.ID = ID; }
            cell.Text = "&nbsp;";
            if (text.Trim() != string.Empty) { cell.Text = text.Trim(); }
            return cell;
        }

        public static TableHeaderCell CreateHeaderCell(string ID, int columns)
        {
            TableHeaderCell cell = CreateHeaderCell(ID);
            cell.ColumnSpan = columns;
            return cell;
        }


        public static TableHeaderCell CreateHeaderCell(string ID, int columns, int rows)
        {
            TableHeaderCell cell = CreateHeaderCell(ID);
            cell.ColumnSpan = columns;
            cell.RowSpan = rows;
            return cell;
        }


        public static TableHeaderCell CreateHeaderCell(string ID, string text, int columns)
        {
            TableHeaderCell cell = CreateHeaderCell(ID, columns);
            cell.Text = "&nbsp;";
            if (text.Trim() != string.Empty) { cell.Text = text.Trim(); }
            return cell;
        }


        public static TableHeaderCell CreateHeaderCell(string ID, string text, int columns, int rows)
        {
            TableHeaderCell cell = CreateHeaderCell(ID, columns,rows);
            cell.Text = "&nbsp;";
            if (text.Trim() != string.Empty) { cell.Text = text.Trim(); }
            return cell;
        }

        public static TableCell CreateCell(string ID)
        {
            TableCell cell = new TableCell();
            if (ID != string.Empty) { cell.ID = ID; }
            return cell;
        }

        public static TableCell CreateCell(string ID, string text)
        {
            TableCell cell = new TableCell();
            if (ID != string.Empty) { cell.ID = ID; }
            cell.Text = "&nbsp;";
            if (text.Trim() != string.Empty) { cell.Text = text.Trim(); }
            return cell;
        }

        public static TableCell CreateCell(string ID, string text, int columns)
        {
            TableCell cell = CreateCell(ID, text);
            cell.ColumnSpan = columns;
            return cell;
        }

        public static TableCell CreateCell(string ID, string text, int columns, int rows)
        {
            TableCell cell = CreateCell(ID, text);
            cell.ColumnSpan = columns;
            cell.RowSpan = rows;
            return cell;
        }

        public static TableCell CreateCell(string ID, int columns)
        {
            TableCell cell = CreateCell(ID);
            cell.ColumnSpan = columns;
            return cell;
        }

        public static TableCell CreateCell(string ID, int columns, int rows)
        {
            TableCell cell = CreateCell(ID);
            cell.ColumnSpan = columns;
            cell.RowSpan = rows;
            return cell;
        }

        public static TableCell CreateCell(string ID, Control control)
        {
            TableCell cell = new TableCell();
            if (ID != string.Empty) { cell.ID = ID; }
            cell.Controls.Add(control);
            return cell;
        }

        public static TableCell CreateCell(string ID, Control control, int columns)
        {
            TableCell cell = CreateCell(ID, control);
            cell.ColumnSpan = columns;
            return cell;
        }

        public static TableCell CreateCell(string ID, Control control, int columns, int rows)
        {
            TableCell cell = CreateCell(ID, control);
            cell.ColumnSpan = columns;
            cell.RowSpan = rows;
            return cell;
        }

        public static TableCell CreateBlankCell(string ID)
        {
            TableCell cell = new TableCell();
            if (ID != string.Empty) { cell.ID = ID; }
            cell.Text = "&nbsp;";
            return cell;
        }

        public static TableCell CreateBlankCell(string ID, int columns)
        {
            TableCell cell = CreateBlankCell(ID);
            cell.ColumnSpan = columns;
            return cell;
        }

        public static TableCell CreateBlankCell(string ID, int columns, int rows)
        {
            TableCell cell = CreateBlankCell(ID);
            cell.ColumnSpan = columns;
            cell.RowSpan = rows;
            return cell;
        }
    }
}

